package com.agile.major.service.impl;

import com.agile.major.domain.CommonCourseOption;
import com.agile.major.repository.CommonCourseOptionRepository;
import com.agile.major.service.CommonCourseOptionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * Service Implementation for managing CommonCourseOption.
 */
@Service
@Transactional
public class CommonCourseOptionServiceImpl implements CommonCourseOptionService{

    private final Logger log = LoggerFactory.getLogger(CommonCourseOptionServiceImpl.class);

    private final CommonCourseOptionRepository commonCourseOptionRepository;

    public CommonCourseOptionServiceImpl(CommonCourseOptionRepository commonCourseOptionRepository) {
        this.commonCourseOptionRepository = commonCourseOptionRepository;
    }

    /**
     * Save a commonCourseOption.
     *
     * @param commonCourseOption the entity to save
     * @return the persisted entity
     */
    @Override
    public CommonCourseOption save(CommonCourseOption commonCourseOption) {
        log.debug("Request to save CommonCourseOption : {}", commonCourseOption);
        return commonCourseOptionRepository.save(commonCourseOption);
    }

    /**
     *  Get all the commonCourseOptions.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<CommonCourseOption> findAll(Pageable pageable) {
        log.debug("Request to get all CommonCourseOptions");
        return commonCourseOptionRepository.findAll(pageable);
    }

    /**
     *  Get one commonCourseOption by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public CommonCourseOption findOne(Long id) {
        log.debug("Request to get CommonCourseOption : {}", id);
        return commonCourseOptionRepository.findOne(id);
    }

    /**
     *  Delete the  commonCourseOption by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete CommonCourseOption : {}", id);
        commonCourseOptionRepository.delete(id);
    }

    @Override
    public List<CommonCourseOption> getCoursesByoptionStatus(String optionStatus) throws Exception {
        if (optionStatus == null) {
            throw new Exception("没有发布状态版本的通识通修课程");
        }
        List<CommonCourseOption> courses = commonCourseOptionRepository.findCommonCourseOptionByOptionStatus(optionStatus);
        return courses;
    }
}
